import java.util.Queue;
import java.util.Scanner;
import java.util.Stack;

class CBLinkedList {
    int val;
    CBLinkedList child;
    CBLinkedList brother;
    public CBLinkedList(int val) {
        this.val = val;
    }
}
public class E2T3 {
    public static TreeNode createTree(int[] nums,int[] degree,int i) {
        if(i>=nums.length) {
            return null;
        }
        TreeNode newNode = new TreeNode(nums[i]);
        if(degree[i]==0) {
            return newNode;
        }else if(degree[i]==1) {
            newNode.left = createTree(nums,degree,2*i);
        }else {
            newNode.left = createTree(nums,degree,2*i);
            newNode.right = createTree(nums,degree,2*i+1);
        }
        return newNode;
    }
    public static CBLinkedList createCBList(TreeNode root,TreeNode bro) {
        if(root==null) {
            return null;
        }
        CBLinkedList newNode = new CBLinkedList(root.val);
        if(root.left==null&&root.right==null&&bro==null) {
            return newNode;
        }
        //找出长子
        if(root.left!=null) {
            newNode.child = createCBList(root.left,root.right);
        }else {
            newNode.child = createCBList(root.right,null);
        }
        //找出兄弟
        newNode.brother = createCBList(bro,null);
        return newNode;
    }
    public static void findPath(CBLinkedList root,StringBuilder stringBuilder) {
        if(root == null) {
            return;
        }
        stringBuilder.append(root.val+" ");
        if(root.child==null&&root.brother==null) {
            System.out.println(stringBuilder);
        }else if(root.child==null) {
            findPath(root.brother,stringBuilder);
        }else if(root.brother==null) {
            findPath(root.child,stringBuilder);
        }else {
            String s = stringBuilder.toString();
            StringBuilder newstringBuilder = new StringBuilder(s);
            findPath(root.child,stringBuilder);
            findPath(root.brother,newstringBuilder);
        }
    }
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int[] nums = new int[n+1];
        int[] degree = new int[n+1];
        for (int i = 1; i <= n; i++) {
            nums[i] = scanner.nextInt();
            degree[i] = scanner.nextInt();
        }
        TreeNode root = createTree(nums,degree,1);
        CBLinkedList head = createCBList(root,null);
        findPath(head,new StringBuilder());
    }
}
